//package com.bank.setting.filter;
//
//import com.alibaba.fastjson.JSONObject;
//import com.bank.setting.standard.result.Result;
//import com.bank.setting.standard.utils.JwtUtil;
//import lombok.extern.slf4j.Slf4j;
//
//import org.springframework.util.AntPathMatcher;
//import org.springframework.util.StringUtils;
//
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
//import static javafx.scene.input.KeyCode.R;
//
//
//@WebFilter(filterName = "LoginCheckFilter",urlPatterns = "/*") // 拦截所有路径
//@Slf4j
//public class LoginCheckFilter implements Filter {
//
//    JwtUtil jwtUtil= new JwtUtil();
//    Result R=new Result<>();
//
//    // 路径匹配器，支持通配符
//    private static final AntPathMatcher antPathMatcher = new AntPathMatcher();
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        HttpServletResponse response = (HttpServletResponse) servletResponse;
//        setResponseMation(response);
//
//        // 1. 获取本次请求的uri
//        String uri = request.getRequestURI();
//        // 2. 判断本次请求是否需要处理,定义不需要处理的路径
//        String[] urls = new String[]{
//            "/stu/login",
//            "/stu/logout",
//            "/stu/register"
//        };
//        boolean check = check(urls, uri);
//
//        // 3. 如果不需要处理
//        if(check){
//            filterChain.doFilter(request, response);
//            return;
//        }
//
//        // 4. 获取请求头中的jwt令牌
//        String jwt = request.getHeader("token");
//
//        // 5. 如果jwt 为空或者null
//        if(!StringUtils.hasLength(jwt)){
//            log.info("请求头为空,jwt解析失败");
//            String noLogin = JSONObject.toJSONString(R.fail("NOT_LOGIN"));
//            // 相应数据
//            response.getWriter().write(noLogin);
//            return;
//        }
//
//        // 6. 校验jwt
//        try{
//            jwtUtil.validateToken(jwt);
//        }catch (Exception e){
//            e.printStackTrace();
//            log.info("jwt解析失败");
//            // 手动转换对象----json------->阿里巴巴fastJSON
//            String noLogin = JSONObject.toJSONString(R.fail("NOT_LOGIN"));
//            response.getWriter().write(noLogin);
//            return;
//        }
//        // 放行
//        log.info("jwt合法放行");
//        filterChain.doFilter(request, response);
//    }
//
//    private void setResponseMation(HttpServletResponse servletResponse) {
//        servletResponse.setCharacterEncoding("UTF-8");
//        servletResponse.setContentType("text/html;charset=UTF-8");
//        servletResponse.setHeader("Access-Control-Allow-Origin", "*");
//        servletResponse.setHeader("Access-Control-Allow-Credentials", "true");
//        servletResponse.setHeader("Access-Control-Allow-Methods", "*");
//        // userToken为用户的token
//        // requestType为请求类型，2为手机端请求，1或者空为PC端请求
//        servletResponse.setHeader("Access-Control-Allow-Headers", "Content-Type,token,loginPCIp,useridentity," +
//            "requestType,x-prototype-version,x-requested-with,X_Requested_With");
//        // 解决跨域时，前台控制台报 Refused to get unsafe header的错
//        servletResponse.addHeader("Access-Control-Expose-Headers", "SESSIONSTATUS,REQUESTMATION,X-JSON");
//        servletResponse.setHeader("Set-Cookie", "HttpOnly;Secure;SameSite=None");
//    }
//
//    /**
//     * */
//    public boolean check(String[] urls, String uri){
//        for (String url : urls) {
//            boolean match = antPathMatcher.match(url, uri);
//            if(match){
//                return true;
//            }
//        }
//        return false;
//    }
//}
